//
//  EFSQuotify.m
//  p4
//
//  Created by caojun on 15/11/19.
//  Copyright (c) 2015年 me.cao-jun. All rights reserved.
//

#import "EFSQuotify.h"

#import "CJQuote.h"

#include <sstream>
#include <iomanip>

using namespace std;

@implementation EFSQuotify

static NSString *DEAFULT_EMPTY_STRING_VALUE = @"--";

+ (NSNumberFormatter *)numberFormatter {
    static NSNumberFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [NSNumberFormatter new];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
        formatter.nilSymbol = DEAFULT_EMPTY_STRING_VALUE;
        formatter.usesGroupingSeparator = NO;
    });
    return formatter;
}

+ (NSNumberFormatter *)percentFormatter {
    static NSNumberFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [NSNumberFormatter new];
        formatter.numberStyle = NSNumberFormatterPercentStyle;
        formatter.nilSymbol = DEAFULT_EMPTY_STRING_VALUE;
        formatter.usesGroupingSeparator = NO;
    });
    return formatter;
}

+ (NSString *)valueToString:(NSNumber *)value {
    return [self valueToString:value withFix:2];
}

+ (NSString *)valueToString:(NSNumber *)value withFix:(NSInteger)toFix {
    if ([value doubleValue] == 0) {
        value = @0;
        return @"--";
    }
    
    NSNumberFormatter *formatter = [self numberFormatter];
    formatter.minimumFractionDigits = formatter.maximumFractionDigits = toFix;
    if ([value doubleValue] >= 0) {
        return [formatter stringForObjectValue:value];
    }
    
    return [NSString stringWithFormat:@"-%@",[formatter stringForObjectValue:@(0 - [value doubleValue])]];
}

+ (NSString *)valueToSymbolString:(NSNumber *)value withFix:(NSInteger)toFix;
{
    NSString *symobl = @"";
    NSString *stringValue = [self valueToString:value withFix:toFix];
    if (value) {
        symobl = [value doubleValue] >= 0 ? @"+" : @"";
    }
    return [NSString stringWithFormat:@"%@%@", symobl, stringValue];
}

+ (NSString *)valueToStringPercent:(NSNumber *)value {
    return [self valueToStringPercent:value withFix:2];
}

+ (NSString *)valueToStringPercent:(NSNumber *)value withFix:(NSInteger)toFix {
    
    NSString *symobl = @"";
    if ([value doubleValue] > 0) {
        symobl = @"+";
    }
    
    NSNumberFormatter *formatter = [self percentFormatter];
    formatter.minimumFractionDigits = formatter.maximumFractionDigits = toFix;
    NSString *resultStr = [formatter stringForObjectValue:@(value.doubleValue / 100)];
    if ([resultStr isEqualToString:@"NaN"] || [resultStr isEqualToString:@"+∞"]) {
        return @"--";
    }
    return [symobl stringByAppendingString:resultStr];
}

+ (NSString *)valueToStringPercentNoPlusSign:(NSNumber *)value withFix:(NSInteger)toFix{
    
    NSString *symobl = @"";
//    if ([value doubleValue] > 0) {
//        symobl = @"+";
//    }
    
    NSNumberFormatter *formatter = [self percentFormatter];
    formatter.minimumFractionDigits = formatter.maximumFractionDigits = toFix;
    NSString *resultStr = [formatter stringForObjectValue:@(value.doubleValue / 100)];
    if ([resultStr isEqualToString:@"NaN"] || [resultStr isEqualToString:@"+∞"]) {
        return @"--";
    }
    return [symobl stringByAppendingString:resultStr];
}

+ (UIColor *)valueToColor:(NSNumber *)value {
    if ([value doubleValue] > 0) {
        return [UIColor bullColor];
    } else if ([value doubleValue] < 0) {
        return [UIColor bearColor];
    }
    return nil;
}

+ (NSString *)valueToHumanReadable:(NSNumber *)value {
    double const TEN_THOUSANDS = 10000;
    double doubleValue = value.doubleValue;
    if (doubleValue < TEN_THOUSANDS) {
        return [NSString stringWithFormat:@"%.0f", doubleValue];
    } else if (doubleValue < TEN_THOUSANDS * TEN_THOUSANDS) {
        return [NSString stringWithFormat:@"%.2f万", doubleValue / TEN_THOUSANDS];
    }
    return [NSString stringWithFormat:@"%.2f亿", doubleValue / TEN_THOUSANDS / TEN_THOUSANDS];
}

+ (NSNumber *)value:(NSNumber *)value scale:(NSInteger)scale roundingMode:(NSRoundingMode)roundingModel
{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingModel scale:scale raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSDecimalNumber *ouncesDecimal;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithDouble:[value doubleValue]];
    
    return [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
}

@end
